package me.jessyan.mvparms.demo.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.xrecyclerview.XRecyclerView;
import com.example.xrecyclerview.refresh.SwipyRefreshLayout;
import com.example.xrecyclerview.refresh.SwipyRefreshLayoutDirection;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.utils.DeviceUtils;
import com.jess.arms.utils.DoubleClickUtils;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.mvparms.demo.di.component.fragment.DaggerShoppingCardComponent;
import me.jessyan.mvparms.demo.di.module.fragment.ShoppingCardModule;
import me.jessyan.mvparms.demo.mvp.contract.fragment.ShoppingCardContract;
import me.jessyan.mvparms.demo.mvp.presenter.fragment.ShoppingCardPresenter;

import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.mvp.ui.activity.shoppcard.BandShopCardActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.shoppcard.CardDetailActivity;
import me.jessyan.mvparms.demo.mvp.ui.activity.shoppcard.InvalidCardActivity;
import me.jessyan.mvparms.demo.mvp.ui.holder.FragShoppingCardHolder;
import me.jessyan.mvparms.demo.mvp.ui.widget.ShadowDrawable;
import me.jessyan.mvparms.demo.utils.NetSnackbarUtil;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 购物卡 一级 fragment
 */
public class ShoppingCardFragment extends OneLevelBaseFragment<ShoppingCardPresenter> implements ShoppingCardContract.View,View.OnClickListener {

    private SwipyRefreshLayout refreshLayout;
    private XRecyclerView recyclerView;
    private TextView invalidTv;
    private TextView emptyTv;
    private FrameLayout conentFl;


    public static ShoppingCardFragment newInstance() {
        ShoppingCardFragment fragment = new ShoppingCardFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerShoppingCardComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .shoppingCardModule(new ShoppingCardModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onReceiveBack() {
        if(isVisible) NetSnackbarUtil.getInstance().checkAndShow(conentFl,getActivity());
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser) NetSnackbarUtil.getInstance().checkAndShow(conentFl,getActivity());
    }


    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =   inflater.inflate(R.layout.home_shopping_card_fragment, container, false);
        recyclerView = view.findViewById(R.id.recyclerView);
        refreshLayout = view.findViewById(R.id.refresh);
        invalidTv=view.findViewById(R.id.invalid_tv);
        emptyTv = view.findViewById(R.id.empty_tv);
        conentFl = view.findViewById(R.id.content_fl);
        invalidTv.setOnClickListener(this);
        view.findViewById(R.id.floatButton).setOnClickListener(this);
        return view;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {


        initRecyclerView();


    }

    private void initRecyclerView() {



        List<String> list = new ArrayList<>();


        LinearLayoutManager lr = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(lr);
        ArmsUtils.configRecyclerView(recyclerView,lr);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.clearHeader();

        DefaultAdapter adapter =
       new DefaultAdapter(list,true) {
            @Override
            public BaseHolder getHolder(View v, int viewType) {
                return new FragShoppingCardHolder(v);
            }

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_frag_card;
            }


        };
        recyclerView.setAdapter(adapter);

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {}

            @Override
            public void onLoadMore() {

            }
        });

refreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh(SwipyRefreshLayoutDirection direction) {
            list.add("s");
            list.add("s");
            list.add("s");
            adapter.notifyDataSetChanged();
    }
});

recyclerView.setEmptyView(emptyTv);

adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener() {
    @Override
    public void onItemClick(View view, int viewType, Object data, int position) {
        Intent intent = new Intent(getActivity(),CardDetailActivity.class);
        intent.putExtra("type",CardDetailActivity.CARD_SHOPPING);

        startActivity(intent);
    }
});
    }


    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void onClick(View view) {
        if(DoubleClickUtils.getInstance().isDouble(view)){
            return;
        }
        switch (view.getId()){
            case R.id.invalid_tv:
                startActivity(new Intent(getActivity(), InvalidCardActivity.class));
                break;
            case R.id.floatButton:
                startActivity(new Intent(getActivity(), BandShopCardActivity.class));
                break;
                default:

        }
    }
}
